#ifndef SOUNDCLOUDSERVER_HPP_
#define SOUNDCLOUDSERVER_HPP_

#include "kit/container/ThreadSafeQueue.hpp"
#include "kit/container/ThreadSafeMap.hpp"
#include "kit/user/UserMap.hpp"
#include "kit/event/Event.hpp"
#include "kit/processor/EventProcessor.hpp"
#include "kit/processor/UserProcessor.hpp"

namespace ServerServer
{
	/**
	 * This class contains the main logic of the application. The application
	 * works on three threads:
	 * - user thread processing incoming user identifications and storing their connections
	 * - event thread processing incoming event messages
	 * - coordinator thread that process incoming events and sends to proper users.
	 * Those threads are created in the start method. Moreover the class manages the lifecycle
	 * of shared resources of threads. In order not to allow access to critical sections by more
	 * than one thread to the resources, I am using resources that are thread safe wrappers over
	 * stl containers implementation. The resources are created and destroyed in this class so
	 * we avoid problems with accessing in one thread, when another already finished.
	 */
	class SoundCloudServer
	{
	public:
		/**
		 * Create the soundcloud server and initialize containers to store information and
		 * processors, responsible for handling tasks.
		 */
		SoundCloudServer();
		/**
		 * Destroy everything what was created in the constructor.
		 */
		~SoundCloudServer();
		/**
		 * The method creates the treads mentioned in the class descriptions and keeps it lifecycle.
		 * The method runs as long as we are receiving events by the EventServer. When it stops we
		 * finish UserServer and Coordinator. When all threads are stopped, we may exit from here.
		 */
		void start();
	private:
		// thread safe queue of events
		KitContainer::ThreadSafeQueue<KitEvent::Event*> * _queue;
		// thread safe map of users id and users itself
		KitUser::UserMap * _map;
		// responsible for processing incoming events
		KitProcessor::EventProcessor * _eventProcessor;
		// responsible for registering users connection
		KitProcessor::UserProcessor * _userProcessor;
	};
}

#endif /* SOUNDCLOUDSERVER_HPP_ */
